#include "bitmap.h"

//datastruct array[1024];
unsigned int bitmap[1024/32];
#define INDEXSFT 5
#define MASK 0x1f
#define BYTEBITS 32
#define ARRAY_SIZE 1024

int test_bit(int i)
{
    int res = bitmap[i >> INDEXSFT] & (1 << (i & MASK));
    return res;
}

int index_of_lst_one_in_bitmap()
{
    int i = 0, test_bits = 0;
    int bits = ARRAY_SIZE/BYTEBITS;
    for (; i < bits; i++) {
        int j = 0;
        for (; j < BYTEBITS; j++) {
            int index = i*BYTEBITS+j;
            if (index > ARRAY_SIZE) {
                return -1;
            } else if (test_bit(index)) {
                return test_bits;
            }
            test_bits ++;
        }
    }
    return -1;
}

void set_one_at_bitmap(int i)
{
    bitmap[i>>INDEXSFT] |= (1 << (i & MASK));
}

void set_zero_at_bitmap(int i)
{
    bitmap[i>>INDEXSFT] &=~(1 << (i & MASK));
}

void init_bd()
{
    int bits = ARRAY_SIZE/BYTEBITS;
    for (int i = 0; i < bits; i++) {
    bitmap[i] = 0xffffffff;
    }
}

